package com.iengoo.spacewar;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-12-26 14:52
 */
public class BombContainer {
    private static List<Bomb> bombList = new ArrayList<>();

    public volatile static BombContainer instance;

    private BombContainer() {
    }

    public static BombContainer getInstance() {
        if (instance == null) {
            synchronized (BombContainer.class) {
                if (instance == null) {
                    instance = new BombContainer();
                }
            }
        }
        return instance;
    }

    public void add(Bomb b) {
        synchronized (bombList) {  // 调用notify 的地方 锁对象必须存在
            bombList.add(b);
        }
    }

    public void empty() {
        synchronized (bombList) {
            bombList.removeAll(bombList);
        }
    }

    public void remove(Bomb b) {
        synchronized (bombList) {
            bombList.remove(b);
        }
    }

    public int size() {
        synchronized (bombList) {
            return bombList.size();
        }
    }

    public Bomb get(int index) {
        synchronized (bombList) {
            return bombList.get(index);
        }
    }
}
